package com.zhuzi;

import cn.hutool.http.HttpRequest;
import org.springframework.util.StringUtils;

public class PingUtils {


    /**
     * ping(如果没有URL返回默认的PONG作为返回值)
     *
     * @param url     url
     * @param pong    默认返回值
     * @param timeout 超时时间
     * @return {@link String}
     */
    public static String ping(String url, String pong, int timeout) {
        if (!StringUtils.hasLength(url)) {
            return pong;
        }
        try {
            String body = HttpRequest.get(url).timeout(timeout).execute().body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pong;
    }

    public static void main(String[] args) {
        String c = PingUtils.ping("http://10.11.20.34:8185/", "c", 1000);
        System.out.println(c);
    }
}
